cmake_minimum_required(VERSION 3.10)
project(my_grpc_test C CXX)

find_package(PCL REQUIRED)

# 引入grpc的cmake文件，其中定义了后面要用到的变量
include(H:/work/grpc/examples/cpp/cmake/common.cmake)

# 获取指定proto的绝对路径存入变量hw_proto
get_filename_component(hw_proto "${CMAKE_CURRENT_SOURCE_DIR}/protos/streaming.proto" ABSOLUTE)
# 获取proto文件所在目录（去掉了文件名）存入变量hw_proto_path
get_filename_component(hw_proto_path "${hw_proto}" PATH)

# 设置变量hw_proto_srcs、hw_proto_hdrs、hw_grpc_srcs、hw_grpc_hdrs
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/streaming.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/streaming.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/streaming.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/streaming.grpc.pb.h")

# 使用命令生成*.pb.h、*.pb.cc、*.grpc.pb.h、*.grpc.pb.cc文件
add_custom_command(
        OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
        COMMAND protoc.exe
        ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
        --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
        -I "${hw_proto_path}"
        --plugin=protoc-gen-grpc="H:/software/grpc/bin/grpc_cpp_plugin.exe"
        "${hw_proto}"
        DEPENDS "${hw_proto}") # DEPENDS说明只有当 ${hw_proto} 文件发生变化时，上述 COMMAND 才会被重新执

# include生成的 *.pb.h 文件
include_directories("${CMAKE_CURRENT_BINARY_DIR}")

# 生成库hw_grpc_proto
add_library(hw_grpc_proto
        ${hw_grpc_srcs}
        ${hw_grpc_hdrs}
        ${hw_proto_srcs}
        ${hw_proto_hdrs})

# 添加库依赖
target_link_libraries(hw_grpc_proto
        ${_REFLECTION}
        ${_GRPC_GRPCPP}
        ${_PROTOBUF_LIBPROTOBUF})


MESSAGE(STATUS ${_REFLECTION})
MESSAGE(STATUS ${_GRPC_GRPCPP})
MESSAGE(STATUS ${_PROTOBUF_LIBPROTOBUF})

foreach(_target
        streaming_client
        streaming_server
        )
    add_executable(${_target} "${_target}.cpp")

    target_include_directories(${_target} PRIVATE ${PCL_INCLUDE_DIRS})

    target_link_libraries(${_target}
            hw_grpc_proto
            absl::flags
            absl::flags_parse
            ${_REFLECTION}
            ${_GRPC_GRPCPP}
            ${_PROTOBUF_LIBPROTOBUF}
            ${PCL_LIBRARIES}
    )
endforeach()
